home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / DEMON / RISCOS2 / TCP_131S.ARC / c / PATHNAME < prev    next >
Text File  |  1992-02-19  |  3KB  |  94 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "global.h"
  5.  
  6. static void crunch(char *, char *);
  7.  
  8. /* Given a working directory and an arbitrary pathname, resolve them into
  9.  * an absolute pathname. Memory is allocated for the result, which
  10.  * the caller must free
  11.  */
  12. char *pathname(char *cd, char *path)
  13. {
  14.         register char *buf,*cp;
  15.  
  16.         if(cd == NULLCHAR || path == NULLCHAR)
  17.                 return NULLCHAR;
  18.         /* Strip any leading white space on args */
  19.         while(*cd == ' ' || *cd == '\t')
  20.                 cd++;
  21.         while(*path == ' ' || *path == '\t')
  22.                 path++;
  23.  
  24.         /* Allocate and initialize output buffer; user must free */
  25.         buf = malloc((unsigned)strlen(cd) + strlen(path) + 10); /* fudge factor */
  26.  
  27.         /* Interpret path relative to cd only if it doesn't begin with "/" */
  28.         if(path[0] != '/')
  29.                 strcpy(buf,cd);
  30.         else
  31.                 strcpy(buf,"$");
  32.  
  33.         crunch(buf,path);
  34.  
  35.         /* Special case: null final path means the root directory */
  36.         if(buf[0] == '\0'){
  37.                 buf[0] = '$';
  38.                 buf[1] = '\0';
  39.         }
  40.  
  41.         /* Translate all /'s to .'s and free temp copies of args */
  42.         if((cp = buf) != NULLCHAR){
  43.                 while((cp = strchr(cp,'/')) != NULLCHAR)
  44.                         *cp = '.';
  45.         }
  46.         return buf;
  47. }
  48.  
  49. /* Process a path name string, starting with and adding to
  50.  * the existing buffer
  51.  */
  52. static void crunch(char *buf, register char *path)
  53. {
  54.         register char *cp;
  55.         
  56.  
  57.         cp = buf + strlen(buf); /* Start write at end of current buffer */
  58.         
  59.         /* Now start crunching the pathname argument */
  60.         for(;;){
  61.                 /* Strip leading /'s; one will be written later */
  62.                 while(*path == '/')
  63.                         path++;
  64.                 if(*path == '\0')
  65.                         break;          /* no more, all done */
  66.                 /* Look for parent directory references, either at the end
  67.                  * of the path or imbedded in it
  68.                  */
  69.                 if(strcmp(path,"..") == 0 || strncmp(path,"../",3) == 0){
  70.                         /* Hop up a level */
  71.                         if((cp = strrchr(buf,'.')) == NULLCHAR)
  72.                                 cp = buf;       /* Don't back up beyond root */
  73.                         *cp = '\0';             /* In case there's another .. */
  74.                         path += 2;              /* Skip ".." */
  75.                         while(*path == '/')     /* Skip one or more slashes */
  76.                                 path++;
  77.                 /* Look for current directory references, either at the end
  78.                  * of the path or imbedded in it
  79.                  */
  80.                 } else if(strcmp(path,".") == 0 || strncmp(path,"./",2) == 0){
  81.                         /* "no op" */
  82.                         path++;                 /* Skip "." */
  83.                         while(*path == '/')     /* Skip one or more slashes */
  84.                                 path++;
  85.                 } else {
  86.                         /* Ordinary name, copy up to next '/' or end of path */
  87.                         *cp++ = '.';
  88.                         while(*path != '/' && *path != '\0')
  89.                                 *cp++ = *path++;
  90.                 }
  91.         }
  92.         *cp++ = '\0';
  93. }
  94.